package yongjian.drp.dao.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import yongjian.drp.dao.UserDao;
import yongjian.drp.model.User;
import yongjian.drp.util.MD5Code;

@Component("userDao")
public class UserDaoImpl implements UserDao{
	
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.UserDao#exist(yongjian.drp.model.User)
	 */
	@Override
	public List<User> exist(User u) {
		List<User> users = new ArrayList<User>();
		if(u==null || u.getUsername() == null || u.getUsername().equals("")){
			return users;
		}
		MD5Code md5 = new MD5Code();
		users = (List<User>)sessionFactory.getCurrentSession().createCriteria(User.class)
					.add(Restrictions.eq("username", u.getUsername()))
						.add(Restrictions.eq("password", md5.getMD5ofStr(u.getPassword()))).list();
		return users;
	}

	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.UserDao#add(yongjian.drp.model.User)
	 */
	@Override
	public void add(User u) {
		u.setCreateDate(new Date());
		sessionFactory.getCurrentSession().save(u);
	}

	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.UserDao#modify(yongjian.drp.model.User)
	 */
	@Override
	public void modify(User u) {
		User user = (User)sessionFactory.getCurrentSession().load(User.class, u.getId());
		user.setUsername(u.getUsername());
		user.setTel(u.getTel());
		user.setAddress(u.getAddress());
		user.setEmail(u.getEmail());
		user.setPosition(u.getPosition());
		sessionFactory.getCurrentSession().update(user);
	}

	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.UserDao#delete(int)
	 */
	@Override
	public void delete(int id) {
		sessionFactory.getCurrentSession().createQuery("delete from User u where u.id=?").setParameter(0, id).executeUpdate();
	}
	
	public String createHql(String key){
		String hql = "from User u where 1=1";
		if(key !=null && !key.equals("")){
			hql += " and u.username like '%" + key +"%'";
		}
		return hql;
	}

	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.UserDao#queryByPage(int, int, java.lang.String)
	 */
	@Override
	public List<User> queryByPage(int page, int rows, String key) {
		String hql = createHql(key);
		List<User> users = (List<User>)sessionFactory.getCurrentSession().createQuery(hql)
							.setFirstResult((page-1)*rows).setMaxResults(rows).list();
		
		return users;
	}
	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.UserDao#getAllUserRecord()
	 */
	@Override
	public int getAllUserRecord(String key) {
		String hql = createHql(key);
		int count = sessionFactory.getCurrentSession().createQuery(hql).list().size();
		return count;
	}
	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.UserDao#modifyPw(yongjian.drp.model.User, java.lang.String)
	 */
	@Override
	public String modifyPw(User u, String oldPassword) {
		MD5Code md5 = new MD5Code();
		User user = (User)sessionFactory.getCurrentSession().get(User.class, u.getId());
		if(!md5.getMD5ofStr(oldPassword).equals(user.getPassword())){
			return "ERROR";
		}
		user.setPassword(md5.getMD5ofStr(u.getPassword()));
		sessionFactory.getCurrentSession().update(user);
		return "SUCCESS";
	}
	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.UserDao#modifyPw(java.lang.String)
	 */
	@Override
	public void modifyPw(String username) {
		MD5Code md5 = new MD5Code();
		String password = md5.getMD5ofStr("11111");
		sessionFactory.getCurrentSession().createQuery("update User u set u.password=? where u.username=?")
					.setString(0, password).setString(1, username).executeUpdate();
	}
	
	

}
